<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2018-02-03 11:11:00 apple $
	消息群发
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$send_to_types=array(
	//'1'=>'发送给指定粉丝',
	'2'=>'发送给指定分组的粉丝',
	'3'=>'发送给所有粉丝',
);
$send_status=array(
	'0'=>'待发送',
	'1'=>'已发送',
);
$content_types=array(
	'0'=>'文本',
	'1'=>'图片',
	'2'=>'语音（音乐）',
	'3'=>'视频',
	'4'=>'图文',
);
$choose_types=array(
	'1'=>'本地素材选取',
	'2'=>'从公众号素材库选取',
);
if($_RQ['op']=='list'){
	//消息群发计划列表
	$params=array('merchid'=>$_SESSION['onemerchid'],'is_wxwork'=>1);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['wxgid'])){
		$params['wxgid']=intval($_RQ['wxgid']);
		$return['params']['wxgid']=$_RQ['wxgid'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['saletask_type'])){
		$params['saletask_type']=intval($_RQ['saletask_type']);
		$return['params']['saletask_type']=$_RQ['saletask_type'];
	}
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	$total='';
	$list=pdo_getslice('core_mass',$params,array($pindex,$psize),$total,array(),'',$orderby);
	if($list){
		foreach($list as &$row){
			$row['tags']=str_array($row['tags']);
			foreach($row['tags'] as $tv){
				$taginfo=pdo_get('jy_scrm_member_group_tag',array('merchid'=>$_SESSION['onemerchid'],'tag_id'=>$tv,'uniacid'=>$_SESSION['uniacid']));
				if($row['tagstitle']){
					$row['tagstitle'] .=';'.$taginfo['title'];
				}else{
					$row['tagstitle']=$taginfo['title'];
				}
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['htmls']=array(
		'type'=>'list',
		'tabs'=>array(
			array('title'=>'群发列表','op'=>'list'),
			array('title'=>'创建群发任务','op'=>'edit'),
		),
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：
		</br>1、注意：调用该接口并不会直接发送消息给客户/客户群，需要企业成员确认后才会执行发送！
		</br>2、同一个企业每个自然月内仅可针对一个客户/客户群发送4条消息，超过接收上限的客户将无法再收到群发消息！
		','delall'=>false,'list'=>array(
			array('type'=>'string','name'=>'title','title'=>'任务名称'),
			array('type'=>'radio','name'=>'saletask_type','title'=>'任务类型','options'=>'saletask_types'),
			array('type'=>'radio','name'=>'content_type','title'=>'群发内容','options'=>'content_types'),
			array('type'=>'radio','name'=>'choose_type','title'=>'素材来源','options'=>'choose_types'),
			array('type'=>'string','name'=>'tagstitle','title'=>'客户标签'),
			array('type'=>'time','name'=>'send_time','title'=>'发送时间'),
			array('type'=>'radio','name'=>'status','title'=>'发送状态','options'=>'send_status'),
		),'action'=>array(
			array('type'=>'post','op'=>'mass','message'=>'确定企业微信推送消息？每月只能推送4条！','title'=>'企业微信推送'),
		)),
		'search_one'=>array('name'=>'title','add_op'=>'edit','select_name'=>'saletask_type','select_title'=>'任务类型','options'=>'wechats'),
	);
	$return['saletask_types']=saletask_types();
	$return['content_types']=$content_types;
	$return['choose_types']=$choose_types;
	$return['send_types']=$send_types;
	$return['send_status']=$send_status;
	exi($return);
}
if($_RQ['op']=='mass'){
	//执行群发
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要群发的任务！','error');
	}
	$mass=pdo_get('core_mass',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	model('core/wxwork_userapi')->member_mass($mass);
	exi('群发成功！');
}
if($_RQ['op']=='sendtime'){
	if($_RQ['postdata']){
		$post=get_postdata();
		exi('');
	}
	$return['item']=array('daysid'=>'0','hoursid'=>'0');
	$htmls=array(
		'type'=>'edit',
		'desc'=>'设置群发时间',
		'edit'=>array(
			array('type'=>'tip','title'=>'你可以选择5分钟后的今、明两天内任意时刻定时群发，成功设置后不支持修改，但在设定的时间之前可取消，取消后不占用群发条数。'),
			array('type'=>'time_list','name'=>'send_time','name1'=>'daysid','name2'=>'hoursid','name3'=>'minsid','title'=>'发送时间'),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='sendnews'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$data=array(
			'wxgid'=>intval($post['wxgid']),			
			'title'=>$post['title'],
			'send_time'=>$post['send_time']?strtotime($post['send_time']):0,
			'sendto'=>intval($post['sendto']),
			'sendto_type'=>intval($post['sendto_type']),
			'lookcontent'=>ijson_encode($post['lookcontent']),
		);
		if(!$data['title']){
			exi('请先设置群发任务标题！','error');
		}
		if($id){
			pdo_update('core_mass',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['merchid']=$_SESSION['onemerchid'];
			$data['createtime']=TIMESTAMP;
			$data['content_type']=4;
			$data['choose_type']=1;
			pdo_insert('core_mass',$data);
			$id=pdo_insertid();
		}
		exi('保存图文成功','','/wechat.mass/edit/id='.$id);
	}
	$id=intval($_RQ['id']);
	$type=intval($_RQ['type']);
	$wxgid=intval($_RQ['wxgid']);
	if($id){
		$item=pdo_get('core_mass',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['lookcontent']=json_decode($item['lookcontent'],true);
		$item['send_time']=date('Y-m-d H:i',$item['send_time']);
		if(!$_SESSION['wechat_id']){
			$_SESSION['wechat_id']=$item['wxgid'];
		}
	}else{
		if(!$wxgid){
			$wxgid=pdo_getcolumn('core_account_wechat',array('uniacid'=>$_SESSION['uniacid'],'isdefault'=>1),'id');
			$wxgid=$wxgid?:pdo_getcolumn('core_account_wechat',array('uniacid'=>$_SESSION['uniacid']),'id');
		}
		$_SESSION['wechat_id']=$wxgid;
		$item=array(
			'wechat_title'=>pdo_getcolumn('core_account_wechat',array('id'=>$_SESSION['wechat_id'],'uniacid'=>$_SESSION['uniacid']),'title'),
			'edit'=>1,
			'wxgid'=>$wxgid,
			'sendto'=>'0',
			'send_timetype'=>'0',
			'content_type'=>4,
			'lookcontent'=>array()
		);
	}
	$item['choose_type']=1;
	$item['title']=$_RQ['title']?:$item['title'];
	$item['wxgid']=$_RQ['wxgid']?:$item['wxgid'];
	$item['sendto']=$_RQ['sendto']?:$item['sendto'];
	$item['send_time']=$_RQ['send_time']?:$item['send_time'];
	$return['item']=$item;
	$htmls[]=array(
		'type'=>'look',
		'width'=>'350px',
		'flex'=>1,
		'look'=>array(
				array('type'=>'wechatmass'),
		),
	);
	$htmls[]=array(
		'type'=>'edit',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回群发列表'),
		),
		'desc'=>'设置群发信息',
		'edit'=>array(
			array('type'=>'string','name'=>'title','title'=>'群发任务标题'),
			array('type'=>'select','name'=>'wxgid','title'=>'选择公众号','options'=>'wechats'),
			array('name'=>'lookcontent','type'=>'choose_many','title'=>'系统文章列表','action'=>'/account.article/get_article/many=1','choose_title'=>'选择系统文章'),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	//添加消息群发计划。如果是编辑群发，该当如何显示已设置的群发内容？文本好显示，图片，语音，视频都好显示，图文如何显示呢？
	$id=intval($_RQ['id']);
	$type=intval($_RQ['type']);
	$wxgid=intval($_RQ['wxgid']);
	if($id){
		$item=pdo_get('core_mass',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['send_time']=date('Y/m/d H:i',$item['send_time']);
		if(!$_SESSION['wechat_id']){
			$_SESSION['wechat_id']=$item['wxgid'];
		}
		if($item['content_type']!=0){
			$item['lookcontent']=$item['lookcontent']?json_decode($item['lookcontent'],true):array();
		}
		if($item['content_type']==1){
			$item['content']=$item['content']?json_decode($item['content'],true):array();
		}
	}else{
		if(!$wxgid){
			$wxgid=pdo_getcolumn('core_account_wechat',array('uniacid'=>$_SESSION['uniacid'],'isdefault'=>1),'id');
			$wxgid=$wxgid?:pdo_getcolumn('core_account_wechat',array('uniacid'=>$_SESSION['uniacid']),'id');
		}
		$_SESSION['wechat_id']=$wxgid;
		$item=array(
			'wechat_title'=>pdo_getcolumn('core_account_wechat',array('id'=>$_SESSION['wechat_id'],'uniacid'=>$_SESSION['uniacid']),'title'),
			'edit'=>1,
			'wxgid'=>$wxgid,
			'sendto'=>'0',
			'send_timetype'=>'0',
			'content_type'=>4,
			'content'=>array(),
			'lookcontent'=>array(),
			'saletask_type'=>'0',
		);
	}
	$item['saletask_set']=$item['saletask_set']?json_decode($item['saletask_set'],true):array('datatype'=>0);
	$return['item']=$item;
	$return['send_types']=$send_types;
	$return['send_to_types']=$send_to_types;
	$return['tags']=pdo_getall('jy_scrm_member_group_tag',array('uniacid'=>$_SESSION['uniacid']),array('tag_id','title'),'tag_id');
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'wxgid'=>intval($postdata['wxgid']),
		'content_type'=>intval($postdata['content_type']),
		'choose_type'=>intval($postdata['choose_type'])?:1,
		'title'=>$postdata['title'],
		'send_time'=>strtotime($postdata['send_time']),
		'tags'=>$postdata['tags'],
		'template_id'=>intval($postdata['template_id']),
		'lookcontent'=>$postdata['lookcontent'],
		'saletask_set'=>ijson_encode($postdata['saletask_set']),
	);
	if(!$data['title']){
		exi('请先设置群发任务标题！','error');
	}
	if(!$postdata['lookcontent']){
		exi('请先设置群发内容！','error');
	}
	$id=intval($postdata['id']);
	$media_ids=array();
	if($_RQ['sendmsg'] && $data['choose_type']==1 && $data['content_type']){
				//直接设置群发内容，如果群发内容不是文本，则上传为素材，并将素材ID存入content字段	
				$wechat=cfc('account',$data['wxgid']);
				$articles=array();
				foreach($postdata['lookcontent'] as $cv){
					if($data['content_type']==1 || $data['content_type']==2){//图片和语音
						$media_id=$wechat->add_media($cv,$data['content_type'],$postdata['title']);
					}
					if($data['content_type']==3){
						$media_id=$wechat->add_media($cv,$data['content_type'],$postdata['title'],$postdata['content_title'],$postdata['content_desc']);
					}
					if(is_error($media_id)){
						exi('上传素材出错：'.$media_id['message'],'error');
					}
					$media_ids[]=$media_id;
				}
				if($data['content_type']==4){//图文素材上传提交
					$media_ids=$wechat->add_media_news($postdata['lookcontent'],$postdata['title'],$id);
				}
				if($data['content_type']==1){//只有图片的多个media_ids
						$data['content']=ijson_encode($media_ids);
				}elseif($data['content_type']!=0){
						$data['content']=$media_ids[0];
				}
	}elseif($data['choose_type']==2){
				if(!$postdata['lookcontent']){
					exi('请先选择群发内容！','error');
				}
				foreach($postdata['lookcontent'] as $cv){
					$media_ids[]=$cv['media_id'];
				}
				if($data['content_type']==1){//只有图片的多个media_ids
						$data['content']=ijson_encode($media_ids);
				}elseif($data['content_type']!=0){
						$data['content']=$media_ids[0];
				}
	}
	if($data['content_type']){//只有文本的不需要整理
		$data['lookcontent']=ijson_encode($postdata['lookcontent']);
	}
	if($id){
		pdo_update('core_mass',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		exi('群发任务修改成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['is_wxwork']=1;
		$data['merchid']=$_SESSION['onemerchid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('core_mass',$data);
		$id=pdo_insertid();
		if($_RQ['sendmsg']){
			if($data['send_time']){
				$plan=array(
					'type'=>1,
					'title'=>'企业微信群发消息',
					'time'=>$data['send_time'],
					'url'=>'jy_merchscrm/member.mass/mass/id='.$id,
					'status'=>1,
				);
				$plan_id=set_plan($plan);
				if(is_error($plan_id)){
					exi($plan_id['message'],'error');
				}
				pdo_update('core_mass',array('plan_id'=>$plan_id),array('id'=>$id));
			}else{
				//cfc('account',$data['wxgid'])->mass($id);
			}
			exi('群发计划增加成功！','','list');
		}else{
			exi('保存成功！','','list');
		}
	}
}
if($_RQ['op']=='log'){
	//发送日志
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$return['list']=pdo_getslice('core_mass',array('uniacid'=>$_SESSION['uniacid'],'status >'=>0),array($pindex,$psize),$total,array(),'id',$orderby);
	if($return['list']){
		foreach($return['list'] as &$row){
			$row['sendtime']=date('y-m-d H:i:s',$row['sendtime']);
			if($row['sendto']){
				$row['sendto']=pdo_getcolumn('core_fansgroup',array('uniacid'=>$_SESSION['uniacid'],'id'=>$row['sendto']),'title');
			}
			$row['wxgid']=pdo_getcolumn('core_account_wechat',array('uniacid'=>$_SESSION['uniacid'],'id'=>$row['wxgid']),'title');
			$row['mediaid']=pdo_getcolumn('core_media',array('uniacid'=>$_SESSION['uniacid'],'id'=>$row['mediaid']),'title');
			$row['status']=($row['status']==1)?'发送成功':'发送失败';
		}
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='saletask_set'){
	if($_RQ['postdata']){
		$post=get_postdata();
		exi('');
	}
	$type=intval($_RQ['type']);
	$id=intval($_RQ['id']);
	if(!$type){
		exi('请先选择要设置的计划！','error');
	}
	if($id){
		$item=pdo_getcolumn('core_mass',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'saletask_set');
	}else{
		$item=array();
	}
	$edit=intval($_RQ['look'])?'look':'edit';
	$item['type']=$type;
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>$edit,
		$edit=>array(
			//array('type'=>'radio','name'=>'type','title'=>'计划类型','options'=>'types'),
			array('type'=>'radio','name'=>'datatype','title'=>'计划数据','options'=>'datatype'),
			array('type'=>'radio','name'=>'ertype','title'=>'日子类型','options'=>'ertype','show'=>array('datatype'=>3)),
			array('type'=>'select','name'=>'holiday_type','title'=>'节日选择','options'=>'holiday_types','show'=>array('ertype'=>3)),
			array('type'=>'radio','name'=>'timesdays','title'=>'选择天数','options'=>'timesdays','show'=>array('datatype'=>1)),
			array('type'=>'radio','name'=>'isjiaoyu','title'=>'消费情况?','options'=>'jiaoyus','show'=>array('datatype'=>1)),
			array('type'=>'radio','name'=>'jiaoyunums','title'=>'消费频率','options'=>'jiaoyunums','show'=>array('datatype'=>1)),
			array('type'=>'radio','name'=>'fans_type','title'=>'粉丝类型','options'=>'fans_type','show'=>array('datatype'=>2)),
		),
	);
	//$return['types']=saletask_types();
	$return['holiday_types']=holiday_types();
	$return['datatype']=array(0=>'无',1=>'订单数据',2=>'粉丝数据',3=>'活动日');
	$return['ertype']=array(0=>'无',1=>'生日',2=>'会员日',3=>'节日');
	$return['jiaoyus']=array('已消费','没订单','有订单未支付');
	$return['timesdays']=array(0=>'默认',7=>'7天内',30=>'30天内',60=>'60天内',90=>'90天内');
	$return['jiaoyunums']=array(0=>'默认',1=>'近期成交高于平均值',2=>'近期成交低于平均值',3=>'历史成交高于平均值',4=>'历史成交低于平均值');
	$return['fans_type']=array('48小时内有交互');
	exi($return);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要删除的任务！','error');
	}
	$mass=pdo_get('core_mass',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('plan_id'));
	if(!$mass){
		exi('指定群发任务不存在！','error');
	}
	if($mass['plan_id']){
		cloud_request('cloud.plan.del',array('id'=>$mass['plan_id']),'','',3);
	}
	pdo_delete('core_mass',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('删除成功！');
}
